package sg.edu.nus.iss.cats.controller;

import java.util.*;

import sg.edu.nus.iss.cats.dataaccess.*;
import sg.edu.nus.iss.cats.model.User;
import sg.edu.nus.iss.cats.model.Employee;

import sg.edu.nus.iss.cats.exception.*;

public class UserSessionManager {

    private static Hashtable sessionTable = new Hashtable();

    public UserSession validate(String userId, String password)
        throws UserNotFoundException, WrongPasswordException, DAOException {

        System.out.println("UserSessionManager: VALIDATING PASSWORD");
        if ((userId == null) || (userId == null)) {
            return null;
        }

        User user = null;

        // Do we already have a session open?
        UserSession s = (UserSession) sessionTable.get(userId);
        if (s != null) {
            user = s.getUser();
        } else {
            UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
            
            System.out.println("UserSessionManager:Going to find user" + userId);
            
            user = userDAO.findUser(userId);
            if (user == null)
                throw new UserNotFoundException("User not found in DB");
        }

        //Validate password
        
        if (!password.equals(user.getPassword())){
            System.out.println("UserSessionManager:Wrong password for : " + userId);
            throw new WrongPasswordException("Wrong password DB");
            
            
         }

        // Validated. If there is no session yet, create it.
        if (s == null) {
            s = createSession(user);
            if (s != null) {
                sessionTable.put(userId, s);
            }
        }
        return s;
    }

    private UserSession createSession(User user)
                        throws UserNotFoundException, DAOException {

        //Create a user session
        UserSession session = new UserSession();
        session.setSessionId(user.getUserId());
        session.setUser(user);

        //Find employee and set in session
        String employeeId = user.getEmployeeId();
        
        System.out.println("createSession: " + user.getUserId() + " + " + user.getEmployeeId());
        
        if (employeeId != null) {
        
            try {
            EmployeeDAO empDAO = DAOFactory.getInstance().getEmployeeDAO();
            
            System.out.println("UserSessionManager: going to find employee" + employeeId);
           
            Employee employee = empDAO.findEmployee(employeeId);
            if (employee == null) {
                throw new UserNotFoundException("Employee record not found in DB");
            }
            session.setEmployee(employee);

            if (session.isManager()) {
                Employee[] subordinates =
                    empDAO.findEmployeesByManager(employeeId);
                session.setSubordinates(subordinates);
            }
            
            } catch (Exception e){
            
                System.out.println("UserSessionManager: exception " + e.toString());
                throw new DAOException(e);
            
            }
        }
        return session;
    }

}